package com.code.generate;

import java.util.Map;

import com.code.generate.enums.ControlType;

/**
 * 
 * MYSQL查询结果生成的字段对象
 * 
 * @author Administrator
 * 
 */
public class MysqlField extends Field {

	public MysqlField(Map<String, Object> map) {

		this.tableField = ("" + map.get("COLUMN_NAME")).trim();
		this.dataType = ("" + map.get("DATA_TYPE")).trim();

		//类型转换
        if (this.dataType.equalsIgnoreCase("DATETIME")){
            this.dataType = "TIMESTAMP";
        }else if (this.dataType.equalsIgnoreCase("int")){
            this.dataType = "INTEGER";
        }

		this.fieldName = CommonUtils.getProperty(("" + map.get("COLUMN_NAME")).trim());
		this.fieldType = CommonUtils.getFieldType(("" + map.get("DATA_TYPE")).trim(), this.tableField);
		if (tableField.equalsIgnoreCase("AUDIT_STATE")) {
			this.fieldType = "AuditState";
			this.fieldName = "auditState";
		}
		if ("NO".equals(("" + map.get("IS_NULLABLE")).trim())) {
			this.whetherRequest = true;
		}
		if ("PRI".equals(("" + map.get("COLUMN_KEY")).trim())) {
			this.whetherPrimaryKey = true;
			this.whetherEdit = false;
			this.whetherList = false;
			this.isUnique = true;
		}
		if ("String".equals(fieldType)) {
			try {
				this.length = new Integer(("" + map.get("CHARACTER_MAXIMUM_LENGTH")).trim()).intValue();
			} catch (java.lang.NumberFormatException e) {
				this.length = 0;
			}

		}
		this.remark = ("" + map.get("COLUMN_COMMENT")).trim();
		if ("String".equals(fieldType)) {
			if (length >= 100) {
				inputType = ControlType.TEXTAREA;
			} else {
				inputType = ControlType.TEXT;
			}
		} else if ("Timestamp".equalsIgnoreCase(dataType)) {
			inputType = ControlType.DATE_TIME;
		} else if ("Date".equalsIgnoreCase(dataType)) {
			inputType = ControlType.DATE;
		} else if ("Time".equalsIgnoreCase(dataType)) {
			inputType = ControlType.TIME;
		} else if ("Boolean".equalsIgnoreCase(dataType)) {
			inputType = ControlType.STATIC_RADIO;
		}
	}

}
